import http from '@/libs/http.js'

//  生成列表需要的item
var generateListItem = (element) => {
  let item = {
    id: element.id,
    username: element.username,
    nickname: element.nickname,
    status: element.status,
    updated_at: element.updated_at,
  }
  return item
}

// 列表接口
export const pageList = (params) => {
  return new Promise((resolve, reject) => {
    http.fetchGet('admin/user', params).then(
      res => {
        let totalItems = []
        res.data.list.forEach(element => {
          let item = generateListItem(element)
          totalItems.push(item)
        })
        resolve({
          totalItems: totalItems,
          data: res.data
        })
      },
      err => {
        reject(err)
      }
    )
  })

}

//  创建
export const create = (params) => {
  return http.fetchPost('admin/user', params)
}

//  获取详情
export const detail = (id) => {
  return http.fetchGet('admin/user/detail/' + id)
}

//  更新
export const update = (id, params) => {
  return http.fetchPut('admin/user/' + id, params)
}

//  删除
export const del = (ids) => {
  if (ids instanceof Array) {
    let params = {ids: ids}
    return http.fetchDelete('admin/user/', params)
  } else {
    return http.fetchDelete('admin/user/' + ids)
  }
}

//  获取角色列表
export const roleList = (params) => {
  return http.fetchGet('admin/user/roleList')
}

export const resetPassword = (params) => {
  return http.fetchPost('admin/user/resetPassword', params)
}

let dataApi = {
  pageList: pageList,
  create: create,
  detail: detail,
  update: update,
  del: del,
  roleList: roleList,
  resetPassword: resetPassword
}
export default dataApi
